package com.ermace.robo.jumper;

import com.ermace.robo.jumper.game.Player.Position;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.ViewGroup.LayoutParams;

public class LoadingView extends SurfaceView implements SurfaceHolder.Callback {

	private int VIEW_WIDTH;
	private int VIEW_HEiGHT;

	private Bitmap Spritesheet;

	private double ROBO_WIDTH;
	private double ROBO_HEIGHT;

	private Position mainPos;
	private int nrOfRobots;

	private int framesPast = 0;
	private int currentFrame = 0;

	private LoadingThread LoadingThread;

	private double SPEED;

	private Paint black;

	private int toShow = 0;
	private int toDraw = 2;
	
	private double previouElapsedTime;
	private double previousTouchTime = 0;

	public LoadingView(Context context, AttributeSet attrs) {
		super(context, attrs);

		getHolder().addCallback(this);

		Spritesheet = BitmapFactory.decodeResource(getResources(), R.drawable.spritesheet_mapselection);

		black = new Paint();

		this.setLayoutParams(new LayoutParams(100, 300));
	}


	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);

		VIEW_WIDTH = w; // store the width
		VIEW_HEiGHT = h; // store the height

		SPEED = w/2;

		ROBO_HEIGHT = h/3;
		ROBO_WIDTH = h/21 * 8;

		mainPos = new Position(VIEW_WIDTH - ROBO_WIDTH, h/3 );

		nrOfRobots = (int) (VIEW_WIDTH / ROBO_WIDTH + 1) + 2;

		black.setColor(Color.BLACK);

	}
	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub

	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {

		LoadingThread = new LoadingThread(getHolder());
		LoadingThread.setRunning(true);
		LoadingThread.start();

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		boolean retry = true;
		LoadingThread.setRunning(false);
		while(retry){
			try {
				LoadingThread.join();
				retry = false;
			} catch (InterruptedException e) {

			}
		}

	}

	public void update(Canvas canvas, double elapsedTimeMS)
	{
		previouElapsedTime += elapsedTimeMS;
		if(previouElapsedTime > 150)
		{
			currentFrame = ++currentFrame % 8;
			previouElapsedTime = 0;
		}

		int height = Spritesheet.getHeight() / 5 ;
		int width = Spritesheet.getWidth() / 9;

		int srcX = currentFrame * width;
		int srcY = toDraw * height;
		
		if (canvas != null) {
			if(toShow == 0)
			{
				canvas.drawRect(new Rect(0,0,canvas.getWidth(), canvas.getHeight()), black);
	
				double interval = elapsedTimeMS / 1000.0;
	
				mainPos = mainPos.add(new Position(interval * SPEED, 0));
	
				if(mainPos.getX() > VIEW_WIDTH)
					mainPos = new Position(VIEW_WIDTH - ROBO_WIDTH, ROBO_HEIGHT );
	
			
	
	
				for(int i = 0; i < nrOfRobots; i ++)
				{
					canvas.drawBitmap(Spritesheet, new Rect((srcX), (srcY), srcX + width,srcY + height), new Rect((int)(mainPos.getX()  - i*ROBO_WIDTH), (int)(mainPos.getY()),
							(int)(mainPos.getX() - i*ROBO_WIDTH + ROBO_WIDTH),(int) (mainPos.getY() + ROBO_HEIGHT)) , null);
				}
			}
			if(toShow == 1)
			{
				
				canvas.drawRect(new Rect(0,0,canvas.getWidth(), canvas.getHeight()), black);
				
				canvas.drawBitmap(Spritesheet, new Rect((srcX), (srcY), srcX + width,srcY + height), new Rect(0, 0,
						(int)(canvas.getWidth()),(int) (canvas.getHeight())) , null);
				
			}
		}
	}

	private class LoadingThread extends Thread {
		private SurfaceHolder surfaceHolder; // for manipulating canvas
		private boolean threadIsRunning = true; // running by default

		// initializes the surface holder
		public LoadingThread(SurfaceHolder holder) {
			surfaceHolder = holder;
			setName("LoadingThread");
		}

		// changes running state
		public void setRunning(boolean running) {
			threadIsRunning = running;
		}

		//controls the game loop
		@Override
		public void run() {

			Canvas canvas = null; // used for drawing

			long previousFrameTime = System.currentTimeMillis();
			while (threadIsRunning) {
				try {
					canvas = surfaceHolder.lockCanvas(null);

					// lock the surfaceHolder for drawing
					synchronized(surfaceHolder) {
						long currentTime = System.currentTimeMillis();
						double elapsedTimeMS = currentTime - previousFrameTime;

						update(canvas, elapsedTimeMS);

						previousFrameTime = currentTime; // update previous time
					}
				} finally {
					if (canvas != null)
						surfaceHolder.unlockCanvasAndPost(canvas);
				}
			}
		}
	}

	public boolean handleEvent(MotionEvent event) {
		if(System.currentTimeMillis() - previousTouchTime > 1000)
		{
			previousTouchTime = System.currentTimeMillis();
			toShow = (toShow + 1) %2;
			toDraw = (toDraw + 2) %4;	
		}
		return false;
	}

}
